package util.translate;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TranslatorRobotNews {

	public void translate() throws ClassNotFoundException, SQLException, IOException {
		Connection conn = null;
		Connection connInsert = null;
		Statement stmt = null;
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		boolean alltime = true;
		Translator translator = new Translator();
		Class.forName("com.mysql.jdbc.Driver");

		while (alltime) {
			conn = DriverManager.getConnection("jdbc:mysql://" + dbhost + "/" + dbname + "?user=root&password=777888999a!&useUnicode=true&characterEncoding=utf8");
			connInsert = DriverManager.getConnection("jdbc:mysql://" + hostDG + "/" + dbnameDG + "?user=root&password=777888999a!&useUnicode=true&characterEncoding=utf8");
			stmt = conn.createStatement();
			rs = stmt.executeQuery("select id,title,smalltext from phome_ecms_news where id > " + workingid + " limit 1000");
			int fullTimes = 0;
			while (rs.next()) {
				String smalltext = new String();
				smalltext = translator.getTString(rs.getString("smalltext"));
				String title = new String();
				title = translator.getTString(rs.getString("title"));
				pstmt = connInsert.prepareStatement("insert into phome_ecms_news (id,smalltext,filename,title) values ( ?,?,?,?)");
				pstmt.setInt(1, rs.getInt("id"));
				pstmt.setString(2, smalltext);
				pstmt.setString(3, RandomString.getRandomString(8) + new SimpleDateFormat("yyyyMMdd").format(new Date()) + new Integer(rs.getInt("id") * 17 + 23).toString());
				pstmt.setString(4, title);
				pstmt.executeUpdate();
				System.out.println(dbname + " --- " + rs.getInt("id"));
				fullTimes++;
				workingid = rs.getInt(1);
			}
			if (fullTimes < 1000) {
				break;
			} else {
				continue;
			}
		}

	}

	String dbhost = new String();

	public void setDbhost(String dbhost) {
		this.dbhost = dbhost;
	}

	public void setDbname(String dbname) {
		this.dbname = dbname;
	}

	public void setHostDG(String hostDG) {
		this.hostDG = hostDG;
	}

	public void setDbnameDG(String dbnameDG) {
		this.dbnameDG = dbnameDG;
	}

	String dbname = new String();
	String hostDG = new String();
	String dbnameDG = new String();
	int workingid = 0;

	public void setWorkingid(int workingid) {
		this.workingid = workingid;
	}

	public int getWorkingid() {
		return workingid;
	}

}
